﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Forms;

namespace TestDxfDocument
{
    class NewTaps//丝锥
    {
        public static void NewTap(Dictionary<string, Object> data)//丝锥
        {
            DxfDocument dxf = new DxfDocument();

            string path = Convert.ToString(data["path"]);
            //double scale = Convert.ToDouble(data["scale"]);
            double scale = 2;


            Hand(dxf, data, scale);//头部
            Shank(dxf, data, scale);//柄部
            fun.DrawFrame(dxf, data, scale, Vector2.Zero);//图框

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);

        }


        private static void Hand(DxfDocument dxf, Dictionary<string, Object> data, double scale)//头部
        {
            //牙型


            #region 图层

            Layer contour = fun.SetLayer("contour");//轮廓层
            Layer dimensioning = fun.SetLayer("dimensioning");//尺寸层
            Layer refin = fun.SetLayer("refin");//基准线/内轮廓/虚线
            Layer center = fun.SetLayer("center");//中心线
            Layer textlayer = fun.SetLayer("textlayer");//文本层
            Layer hatching = fun.SetLayer("hatching");//填充层
            Layer additionalline = fun.SetLayer("additionalline");//附加线
            Layer cutting = fun.SetLayer("cutting");//切削层

            #endregion

            #region 参数

            double offset = 10;
            string path = Convert.ToString(data["path"]);


            double lu = 26;
            double thl = 16;
            double td = 8;
            double tcl = 2;
            double tp = 1;
            double h = 0.866025 * td;

            #endregion

            #region 点

            //主图
            Vector2 v0 = Vector2.Zero;
            Vector2 vr0 = new Vector2(lu + offset + td * 2, 0);
            Vector2 v1 = new Vector2(0, td / 2 - h);
            Vector2 v2 = new Vector2(v1.X - (lu - thl) / 2, td / 4);
            Vector2 v3 = new Vector2(v1.X - (lu - thl), v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y - td / 2 * 0.1);
            Vector2 v5 = new Vector2(v3.X, 0);
            Vector2 v6 = new Vector2(v3.X - tp / 2, td / 2);
            Vector2 v7 = new Vector2(v3.X - tp, v3.Y);
            Vector2 v8 = new Vector2(-lu, v1.Y);
            Vector2 v9 = new Vector2(v8.X, v4.Y);
            Vector2 v10 = new Vector2(v9.X, (td / 2 - h) / 2);
            Vector2 v11 = new Vector2(v8.X, 0);
            Vector2 v12 = new Vector2(v11.X - v10.Y, 0);
            Vector2 v13 = fun.Polar2(vr0, td / 2, 90 - 360 / 6);
            Vector2 v14 = fun.Polar2(vr0, td / 2 - h, 90 - 360 / 6);
            Vector2 v15 = fun.Polar2(vr0, td / 2, 90);
            Vector2 v16 = fun.Polar2(vr0, td / 2 - h, 90);
            Vector2 v17 = fun.Polar2(vr0, td / 2, 90 + 360 / 6);
            Vector2 v18 = fun.Polar2(vr0, td / 2 - h, 90 + 360 / 6);

            #endregion

            #region 线
            //主图
            LwPolyline lw1 = fun.AddPointsToLwPolyLine(v0, v3, v4);
            LwPolyline lw2 = fun.AddPointsToLwPolyLine(v3, v6, v7);
            LwPolyline lw3 = fun.AddPointsToLwPolyLine(v8, v11, v10, v12);
            LwPolyline lw4 = fun.AddPointsToLwPolyLine(v5, v2, v4);

            LwPolyline lwm1 = fun.MirrorPolyline(lw1, v0, v5);
            LwPolyline lwm2 = fun.MirrorPolyline(lw2, v0, v5);
            LwPolyline lwm3 = fun.MirrorPolyline(lw3, v0, v5);

            //左图


            #endregion

            #region 标注



            #endregion


            #region 编辑



            #endregion

        }

        private static void Shank(DxfDocument dxf, Dictionary<string, Object> data, double scale)//柄部
        {
            //DIN 371
            //DIN 376
            //DIN-ANSI
            //JIS-B-4430

            #region 图层

            Layer contour = fun.SetLayer("contour");//轮廓层
            Layer dimensioning = fun.SetLayer("dimensioning");//尺寸层
            Layer refin = fun.SetLayer("refin");//基准线/内轮廓/虚线
            Layer center = fun.SetLayer("center");//中心线
            Layer textlayer = fun.SetLayer("textlayer");//文本层
            Layer hatching = fun.SetLayer("hatching");//填充层
            Layer additionalline = fun.SetLayer("additionalline");//附加线
            Layer cutting = fun.SetLayer("cutting");//切削层

            #endregion

            #region 参数

            double textHeight = 3.5;
            double offset = 10;
            double pi = Math.PI;
            double rad = Math.PI / 180;
            string path = Convert.ToString(data["path"]);
            scale = 5;

            double oal = 44 * scale;
            double drvl = 4 * scale;
            double drvsc = 3.6 * scale;
            double dconms = 4 * scale;
            double lu = 26 * scale;

            #endregion

            #region 点
            //主图
            Vector2 v0 = Vector2.Zero;
            Vector2 v1 = new Vector2(0, dconms / 2);
            Vector2 v2 = new Vector2(v1.X + oal - lu - drvl, v1.Y);
            Vector2 v3 = new Vector2(v2.X, dconms / 2 * 0.1);
            Vector2 v4 = new Vector2(v1.X + oal - lu - drvl / 2 * 0.2, v1.Y);
            Vector2 v5 = new Vector2(v4.X, dconms / 2 * 0.1);
            Vector2 v6 = new Vector2(v4.X, 0);
            Vector2 v7 = new Vector2(v0.X + oal - lu, Vector2.MidPoint(v4, v5).Y / 2);
            Vector2 v8 = new Vector2(v7.X, 0);

            //右图
            Vector2 v9 = new Vector2(v8.X + offset + dconms * 2, 0);
            Vector2 v10 = new Vector2(v9.X + dconms / 2, 0);
            Vector2 v11 = new Vector2(v9.X, dconms / 2);
            Vector2 v12 = new Vector2(v9.X - dconms / 2, 0);
            Vector2 v13 = new Vector2(v9.X, -v11.Y);

            #endregion

            #region 线

            //主图
            Line linec1 = new Line(new Vector2(v2.X + drvl / 2, v2.Y + offset), new Vector2(v2.X + drvl / 2, v2.Y - offset - dconms));
            LwPolyline lw1 = Pus.AddPointsToLwPolyLine(v0, v1, v4, v2, v3, v5, v6);
            Line line1 = new Line(v7, v8);
            Arc arc1 = fun.DrawArc3(v5, v7, v4);

            LwPolyline lwm1 = fun.MirrorPolyline(lw1, v0, v8);
            Line linem1 = fun.MirrorLine(line1, v0, v8);
            Arc arcm1 = fun.MirrorArc(arc1, v0, v8);

            //右图

            LwPolyline lw2 = Pus.AddPointsToLwPolyLine(v10, v11, v12, v13);
            lw2.IsClosed = true;
            Pus.AglToArc(lw2, dconms * 0.1);

            fun.AddHatch(dxf, hatching, lw2, new Line[32], new Arc[32], 45, offset);

            #endregion

            #region 标注

            fun.DimensionLiner(dxf, dimensioning, v8, new Vector2(v2.X, -v2.Y), offset * 2 + drvl / 4, 180, "DRVL=" + drvl);
            fun.DimensionLiner(dxf, dimensioning, v4, new Vector2(v4.X, -v4.Y), -offset, 90, "DCONMS=" + dconms);
            fun.DimensionLiner(dxf, dimensioning, v10, v13, -offset, 45, "DRVSC=" + drvsc);
            fun.jt(dxf, new Vector2(v2.X + drvl / 2, v2.Y + offset), 180, textHeight, textHeight, "A");
            fun.jt(dxf, new Vector2(v2.X + drvl / 2, v2.Y - dconms - offset), 180, textHeight, textHeight, "A");
            fun.text(dxf, new Vector2(v11.X, v11.Y + offset), textHeight, "A-A");

            #endregion


            #region 编辑

            fun.AddEntities(dxf, center, linec1);
            fun.AddEntities(dxf, contour, lw1, lwm1, line1, linem1, arc1, arcm1, lw2);

            #endregion
        }



    }
}
